using System;
using System.Collections;
using System.Data;
using System.Configuration;
using System.Web.Security;
using MJHDataService;

/// <summary>
/// Summary description for AppUser
/// </summary>
public class AppUser :IDisposable
{
    private bool disposed = false;
    private SqlDataService oSvc;

    public AppUser()
    {
        oSvc = new SqlDataService(ConfigurationManager.ConnectionStrings["NuTraxDB"].ConnectionString);
    }

    #region IDisposable Members
    private void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
            {
                oSvc.Dispose();
            }
            disposed = true;
        }
    }
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    ~AppUser()
    {
        Dispose(false);
    }
    #endregion

    public Hashtable Login(string emailAddress, string password, string ipAddress, DateTime loginDate)
    {
        string sHashPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5");

        Hashtable oParam = new Hashtable();
        oParam.Add("EmailAddress", emailAddress);
        oParam.Add("Password", sHashPassword);
        oParam.Add("IPAddress", ipAddress);
        oParam.Add("LoginTime", loginDate);

        return oSvc.ExecuteProcedureReturnHashtable("AppUser_Login", oParam);
    }
    public Hashtable GetAppUserInfo(int appUserID)
    {
        Hashtable oParam = new Hashtable();
        oParam.Add("AppUserID", appUserID);
        return oSvc.ExecuteProcedureReturnHashtable("AppUser_Read_Item", oParam);
    }
    public void UpdateInfo(int appUserID, string firstName, string lastName, string emailAddress,string password, short timezoneOffset,bool approved, bool updatePassword,bool deleted)
    {
        Hashtable oParam = new Hashtable();
        oParam.Add("AppUserID", appUserID);
        oParam.Add("FirstName", firstName);
        oParam.Add("LastName", lastName);
        oParam.Add("EmailAddress", emailAddress);
        oParam.Add("Password", password);
        oParam.Add("TimeZoneOffset", timezoneOffset);
        oParam.Add("Approved", approved);
        oParam.Add("UpdatePassword", updatePassword);
        oParam.Add("Deleted", deleted);
        UpdateInfo(oParam);
    }
    public void UpdateInfo(Hashtable oParam)
    {
        oSvc.ExecuteNonQuery("AppUser_Update_Item", oParam);
    }
    public DataTable GetAppRoles(int appRoleID)
    {
        Hashtable oParam = new Hashtable();
        oParam.Add("AppUserID", appRoleID);
        return oSvc.ExecuteProcedureReturnDS("AppUser_AppRole_Read_List", oParam).Tables[0];
    }
    public DataTable GetMeasurementPreference(int appUserID)
    {
        Hashtable oParam = new Hashtable();
        oParam.Add("AppUserID", appUserID);
        return oSvc.ExecuteProcedureReturnDS("AppUser_MeasurementUnit_Read_List", oParam).Tables[0];
    }
    public void SaveMeasurementPreference(int appUserID, int measurementUnitID)
    {
        Hashtable oParam = new Hashtable();
        oParam.Add("AppUserID", appUserID);
        oParam.Add("MeasurementUnitID", measurementUnitID);
        oSvc.ExecuteNonQuery("AppUser_MeasurementUnit_Update", oParam);
    }
}
